﻿using In66.Net.EventBus.Cap;

namespace In66.Net.Share.Application.Tracker
{
    public sealed class MessageTrackerFactory
    {
        public IEnumerable<IMessageTracker> _trackers;
        public MessageTrackerFactory(IEnumerable<IMessageTracker> trackers)
        {
            _trackers = trackers;
        }

        public IMessageTracker Create(TrackerKind kind = TrackerKind.Db)
        {
            if (_trackers.IsBusinessNullOrEmpty())
            {
                return new NullMessageTrackerService();
            }

            return _trackers.FirstOrDefault(x => x.Kind == kind) ?? new NullMessageTrackerService();
        }
    }

    public static class MessageTrackerExtension
    {
        public static async Task<bool> HasProcessedAsync<T>(this IMessageTracker tracker, Event<T> eventDto)
            where T : class
        {
            return await tracker.HasProcessedAsync(eventDto.Id, eventDto.EventTarget);
        }

        public static async Task MarkAsProcessedAsync<T>(this IMessageTracker tracker, Event<T> eventDto)
        where T : class
        {
            await tracker.MarkAsProcessedAsync(eventDto.Id, eventDto.EventTarget);
        }
    }
}
